package tree.binary;

import tree.Tree;

/**
 * 二叉树
 *
 * @Author: wangxiaonan
 * @Date: 2019/3/4
 **/
public class BinaryTreeNode implements Tree<BinaryTreeNode> {

    Object data; // 数据
    BinaryTreeNode left;// 左子树
    BinaryTreeNode right;// 右子树

    public BinaryTreeNode(Object data) {
        this.data = data;
    }

    public BinaryTreeNode(Object data, BinaryTreeNode left, BinaryTreeNode right) {
        this.data = data;
        this.left = left;
        this.right = right;
    }

    /**
     * |            8
     * |        4         9
     * |    1      5   7
     * |      3  6       2
     */
    public static BinaryTreeNode initBinaryTree() {
        BinaryTreeNode I = new BinaryTreeNode("2");
        BinaryTreeNode H = new BinaryTreeNode("6");
        BinaryTreeNode G = new BinaryTreeNode("3");
        BinaryTreeNode F = new BinaryTreeNode("7");
        BinaryTreeNode E = new BinaryTreeNode("5");
        BinaryTreeNode D = new BinaryTreeNode("1");
        BinaryTreeNode C = new BinaryTreeNode("9");
        BinaryTreeNode B = new BinaryTreeNode("4");
        BinaryTreeNode root = new BinaryTreeNode("8");
        root.setLeft(B);
        root.setRight(C);
        B.setLeft(D);
        B.setRight(E);
        C.setLeft(F);
        F.setRight(I);
        D.setRight(G);
        E.setLeft(H);
        return root;
    }

    @Override
    public BinaryTreeNode getLeft() {
        return left;
    }

    public void setLeft(BinaryTreeNode left) {
        this.left = left;
    }

    @Override
    public BinaryTreeNode getRight() {
        return right;
    }

    public void setRight(BinaryTreeNode right) {
        this.right = right;
    }

    @Override
    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

}
